package com.ruitu.health_2.ui.activity;

import android.os.Bundle;
import android.view.View;

import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.ModelModel;
import com.ruitu.health_2.mvp.presenter.ModelPresenter;
import com.ruitu.health_2.ui.adapter.CommonProListAdapter;
import com.ruitu.health_2.ui.adapter.ModelArtManListAdapter;
import com.ruitu.health_2.ui.adapter.ModelArticleListAdapter;
import com.ruitu.health_2.ui.adapter.ModelVideoListAdapter;
import com.ruitu.health_2.ui.common.BaseModelActivity;
import com.ruitu.router_module.bean.home.AdvertContent;
import com.ruitu.router_module.bean.home.ModelData;
import com.ruitu.router_module.support.util.GlideImageLoader;
import com.ruitu.router_module.util.GlideUtil;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

// 健康管理首页
public class HealthManageActivity extends BaseModelActivity<ModelPresenter.HealthManageP, ModelModel.HealthManageM> {

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_health_manage;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 艺人
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        artManAdapter = new ModelArtManListAdapter(this);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(artManAdapter);
        // 视频
        GridLayoutManager manager2 = new GridLayoutManager(this, 2);
        videoAdapter = new ModelVideoListAdapter(this);
        rcv_list_2.setLayoutManager(manager2);
        rcv_list_2.setAdapter(videoAdapter);
        // 文章
        LinearLayoutManager manager3 = new LinearLayoutManager(this);
        articleAdapter = new ModelArticleListAdapter(this);
        rcv_list_3.setLayoutManager(manager3);
        rcv_list_3.setAdapter(articleAdapter);
        // 商品
        GridLayoutManager manager4 = new GridLayoutManager(this, 2);
        proAdapter = new CommonProListAdapter(this);
        rcv_list_4.setLayoutManager(manager4);
        rcv_list_4.setAdapter(proAdapter);

        setListeners();
        setHeadTitle("健康管理");

        rxList.add(p.reqModelData("6"));// 请求模块数据
        showProgress();

    }

    private void setListeners() {
        proAdapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", proList.get(position).getId());
                startActivity(ProductDetailActivity.class, bundle);
            }
        });
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {// 获取模块的数据成功
            modelData = (ModelData) data;
            if (null != data) {
                bannerList = modelData.getAdvertCarouselList();
                initBanner(bannerList);
                try {
                    GlideUtil.loadImg(modelData.getAdvertShowAndGuideList().get(0), iv_1);
                    GlideUtil.loadImg(modelData.getAdvertShowAndGuideList().get(1), iv_2);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                artManList = modelData.getArtManList();
                videoList = modelData.getVideoList();
                articleList = modelData.getArticleList();
                proList = modelData.getProductList();

                artManAdapter.setDataList(artManList);
                articleAdapter.setDataList(articleList);
                videoAdapter.setDataList(videoList);
                proAdapter.setDataList(proList);
            }
        }
        super.onReqComplete(code, isOk, data);
    }

    private void initBanner(final List<AdvertContent> bannerList) {
        List<String> urlList = new ArrayList<>();//图片地址集合
        if (!isDataEmpty(bannerList)) {
            for (AdvertContent banner : bannerList) {
                urlList.add(banner.getImgUrl());
            }
        }
        if (isDataEmpty(urlList)) {
            banner.setVisibility(View.GONE);
            return;
        } else {
            banner.setVisibility(View.VISIBLE);
        }
        try {
            banner.setImages(urlList);//设置图片集合
            banner.setImageLoader(new GlideImageLoader());//设置图片加载器
            banner.setDelayTime(6000);//设置轮播时间
            banner.setIndicatorGravity(BannerConfig.RIGHT);
            banner.setBannerAnimation(Transformer.Accordion);
            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int pos) {
                    ToastUtils.showShortSafe("点击广告banner" + pos);
                }
            });
            banner.start();// banner设置方法全部调用完毕时最后调用
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
